package com.example.demo.services;

import com.example.demo.entity.Pick.*;
import com.example.demo.repository.PickRepository;
import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class PickService {
    @Autowired
    private PickRepository repo;
    @Autowired
    private EntityManager entityManager;

    public List<PickDTO> getPickProduceWpTakeGoods(String billNo) {
        List<PickProduceWpTakeGoods> picks = repo.getAllByProductBillNo(billNo);
        List<PickDTO> pickDTOList = new ArrayList<>();
        for (PickProduceWpTakeGoods p : picks) {
            System.out.println(picks);
            System.out.println(p);
            System.out.println(pickDTOList);
            List<PickDTO> pickDTO = repo.getPickDetail(p.getProductBillNo(), p.getWpID(), p.getGoodsCode());
            System.out.println(pickDTO);
            pickDTOList.addAll(pickDTO);
        }
        System.out.println(pickDTOList);
        return pickDTOList;
    }

    @Transactional
    public int saveAll(PickVO pickVO){
        try {
            PickMaster master = pickVO.getMaster();
            for (PickDTO p : pickVO.getDetail()) {
                PickDetail pickDetail = new PickDetail();
                int wpID = repo.getWpIDByWpCode(p.getProductBillNo(), p.getGoodsCode(), p.getWpCode());
                pickDetail.setBillNo(master.getBillNo());
                pickDetail.setProductBillNo(p.getProductBillNo());
                pickDetail.setGoodsCode(p.getGoodsCode());
                pickDetail.setWpID(wpID);
                pickDetail.setQty(p.getQty());
                int cases = repo.checkDetail(pickDetail.getBillNo(), pickDetail.getProductBillNo(), pickDetail.getGoodsCode(), pickDetail.getWpID());
                if(cases == 1) {
                    repo.updateDetail(pickDetail.getBillNo(), pickDetail.getProductBillNo(), pickDetail.getGoodsCode(), pickDetail.getWpID(),pickDetail.getQty());
                }else if(cases == 0) {
                    entityManager.persist(pickDetail);
                }else {
                    System.out.println("error");
                    return 0;
                }

            }
            int cases2 = repo.checkMaster(master.getBillNo());
            if(cases2 == 1){
                repo.updateMaster(master.getBillNo(),
                        master.getInOutDate(),
                        pickVO.getWorkingAreaCode(),
                        pickVO.getTakeReturnWorkingAreaCode(),
                        pickVO.getTakeReturnPerson(),
                        pickVO.getWorkerCode()
                );
            }else if (cases2 == 0) {
                repo.saveMaster(master.getBillNo(),
                        master.getBillStatus(),
                        master.getBillType(),
                        master.getInOutDate(),
                        pickVO.getWorkingAreaCode(),
                        pickVO.getTakeReturnWorkingAreaCode(),
                        master.getCreateBy(),
                        master.getCreateDate(),
                        pickVO.getStockpreparationStatus(),
                        pickVO.getTakeReturnPerson(),
                        pickVO.getWorkerCode()
                );
            }else {
                System.out.println("error2");
                return 0;
            }

        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    public String getWorker(String code) {
        return repo.getWorker(code);
    }
}
